package com.inspur.gs.sbssdl.sbwhgl.sbwhby.sbwhby.back.newsbbyjh.validations;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.bef.spi.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.spi.action.validation.AbstractValidation;
import com.inspur.edp.bef.api.action.validation.IValidationContext;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.entity.entity.IEntityData;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.ArrayList;
import java.util.List;

public class newsbbyjhValBeforeSave1MgrAction extends AbstractValidation {
	public newsbbyjhValBeforeSave1MgrAction(IValidationContext context, IChangeDetail change) {
		super(context,change);
	}
	@Override
	public  void execute() {
		//校验同年的同设备
		IEntityData currentData = this.getContext().getCurrentData();
		String id = currentData.getID();
		String year = currentData.getValue("year").toString();
		String sbbh = currentData.getValue("sbbh").toString();



		SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
		try {
			//点检配置变更
			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
			String byjhid = "48950655-b905-4cb2-b359-9ca407386764";// 设备点检配置id
			IStandardLcp byjhlcp = iLcpFactory.createLcpByBEId(byjhid);

			EntityFilter filter = new EntityFilter();
			ArrayList<FilterCondition> filterConditions = (ArrayList<FilterCondition>) FilterCondition.builder()
					.orEqual("year", year)
					.andEqual("sbbh", sbbh)
					.andNotEqual("id",id)
					.build();
			filter.addFilterConditions(filterConditions);
			List<IEntityData> query = byjhlcp.query(filter);
			if (!query.isEmpty()){
				throw new RuntimeException("该设备此年份已添加，请重新添加设备或修改年份！");
			}

		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}

	}
	private IEntityData getData() {
		return (IEntityData)super.getContext().getCurrentData();
	}
}